<?php

/**
 * This is the model class for table "{{color}}".
 *
 * The followings are the available columns in table '{{color}}':
 * @property integer $id
 * @property integer $createddate
 * @property integer $modifieddate
 * @property integer $enabled
 * @property integer $deleted
 */
class Color extends CActiveRecord
{
  /**
   * Returns the static model of the specified AR class.
   * @param string $className active record class name.
   * @return Color the static model class
   */
  public $name;
  public $name2;
  public $languageID;
  public static function model($className=__CLASS__)
  {
    return parent::model($className);
  }

  /**
   * @return string the associated database table name
   */
  public function tableName()
  {
    return '{{color}}';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules()
  {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
    array('enabled, name', 'required'),
    array('createddate, modifieddate, enabled, deleted', 'numerical', 'integerOnly'=>true),
    // The following rule is used by search().
    // Please remove those attributes that should not be searched.
    array('id, createddate, modifieddate, enabled, deleted', 'safe', 'on'=>'search'),
    );
  }

  /**
   * @return array relational rules.
   */
  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
		  'colorLangs' => array(self::HAS_MANY, 'ColorLang', 'colorID'),
      'products' => array(self::HAS_MANY, 'Product', 'colorID'),
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels()
  {
    return array(
			'id' => 'ID',
		  'name' => Yii::t('default', 'Color'),
			'createddate' => Yii::t('default', 'Createddate'),
			'modifieddate' => Yii::t('default', 'Modifieddate'),
			'enabled' => Yii::t('default', 'Enabled'),
			'deleted' => 'Deleted',
    );
  }

  protected function beforeSave() {
    if($this->isNewRecord) {
      $this->createddate = time();
    }
    $this->modifieddate = time();
    return parent::beforeSave();
  }

  public function getColorList()
  {
    $array = array();

    $colors = Yii::app()->db->createCommand()
    ->select('c.id , cl.name')
    ->from('tbl_color as c')
    ->join('tbl_color_lang as cl', 'cl.colorID = c.id')
    ->where('c.deleted=0 AND cl.languageID="'.Yii::app()->session['language'].'"')
    ->queryAll();
     
    if(!empty($colors)) {
      foreach ($colors as $c) {
        $array[$c['id']] = $c['name'];
      }
    }
    return $array;

  }


  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search()
  {
    $criteria=new CDbCriteria;
    $criteria->together = true;
    $criteria->select = 't.id, l.name, t.createddate, t.modifieddate, t.enabled';
    $criteria->join = 'JOIN tbl_color_lang l ON t.id=l.colorID';
    $criteria->condition = 't.deleted=0 AND l.languageID="'.Yii::app()->session['language'].'"';
    $criteria->compare('id',$this->id);
    $criteria->compare('enabled',$this->enabled);

    return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		  'sort'=>array(
        'attributes'=>array(
            'name'=>array(
                'asc'=>'l.name',
                'desc'=>'l.name DESC',
    ),
            '*',
    ),
    ),
    ));
  }
}